package com.cskaoyan.utils;

import com.cskaoyan.bean.MarketLog;
import com.cskaoyan.bean.common.BaseRespVo;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @Author: JianhuaZeng
 * @Date: 2022/12/30 23:16
 */
public class HttpUtil {

    /**
     * @description: 获取ip
     * @return: java.lang.String
     * @author: JianhuaZeng
     * @date: 2022/12/31 2022/12/31
     */
    public static String getIp() {
        ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = sra.getRequest();
        if (request == null) {
            return "unknown";
        }
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ip;
    }

    /**
     * @description: 根据BaseRespVo.errno是否==0返回truefalse
     * @param: baseRespVo
     * @return: boolean
     * @author: JianhuaZeng
     * @date: 2022/12/31 2022/12/31
     */
    public static boolean getStatus(Object baseRespVo) {
        BaseRespVo respVo = (BaseRespVo) baseRespVo;
        if (respVo.getErrno() == 0) {
            return true;
        }
        return false;
    }

    /**
     * @description: 根据uri返回日志中的type
     * @return: int
     * @author: JianhuaZeng
     * @date: 2022/12/31 2022/12/31
     */
    public static int getType() {
        ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = sra.getRequest();
        String uri = request.getRequestURI();
        int type;
        if (uri.startsWith("/admin/auth/") || uri.startsWith("/admin/admin/")) {
            type = 1;
        } else if (uri.startsWith("/admin/order/")) {
            type = 2;
        } else {
            type = 0;
        }
        return type;
    }

    /**
     * @description: 返回日志对象
     * @param: res
     * @param: action
     * @param: result
     * @return: com.cskaoyan.bean.MarketLog
     * @author: JianhuaZeng
     * @date: 2022/12/31 2022/12/31
     */
    public static MarketLog addLog(Object res, String admin, String action, String result) {
        Date date = new Date();
        String ip = getIp();
        Integer type = getType();
        boolean status = getStatus(res);
        String comment = null;
        Date add_time = date;
        Date update_time = date;
        boolean deleted = false;
        return new MarketLog(null, admin, ip, type, action, status, result, comment, add_time, update_time,
                deleted);
    }
}
